package com.houweishi.module.outbound.service.impl;

import java.util.List;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.module.outbound.domain.BizOutboundProduct;
import com.houweishi.module.outbound.mapper.BizOutboundProductMapper;
import com.houweishi.module.product.domain.BizProduct;
import com.houweishi.module.product.domain.BizProductModel;
import com.houweishi.module.product.mapper.BizProductMapper;
import com.houweishi.module.product.mapper.BizProductModelMapper;
import com.houweishi.module.stock.domain.BizStock;
import com.houweishi.module.stock.mapper.BizStockMapper;
import org.springframework.stereotype.Service;
import com.houweishi.module.outbound.mapper.BizOutboundMapper;
import com.houweishi.module.outbound.domain.BizOutbound;
import com.houweishi.module.outbound.service.IBizOutboundService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 出库记录Service业务层处理
 * 
 * @author houweishi
 * @date 2023-02-25
 */
@Service
public class BizOutboundServiceImpl implements IBizOutboundService 
{
//    @Autowired
    @Resource
    private BizOutboundMapper bizOutboundMapper;

    @Resource
    private BizOutboundProductMapper bizOutboundProductMapper;

    @Resource
    private BizStockMapper bizStockMapper;

    @Resource
    private BizProductModelMapper bizProductModelMapper;

    @Resource
    private BizProductMapper bizProductMapper;

    /**
     * 查询出库记录
     * 
     * @param id 出库记录主键
     * @return 出库记录
     */
    @Override
    public BizOutbound selectBizOutboundById(Long id)
    {
        BizOutbound bizOutbound = bizOutboundMapper.selectBizOutboundById(id);

        BizOutboundProduct bizOutboundProduct = new BizOutboundProduct();
        bizOutboundProduct.setOutboundId(id);
        List<BizOutboundProduct> productList = bizOutboundProductMapper.selectBizOutboundProductList(bizOutboundProduct);

        bizOutbound.setBizOutboundProductList(productList);
        return bizOutbound;
    }

    /**
     * 查询出库记录列表
     * 
     * @param bizOutbound 出库记录
     * @return 出库记录
     */
    @Override
    public List<BizOutbound> selectBizOutboundList(BizOutbound bizOutbound)
    {
        List<BizOutbound> list = bizOutboundMapper.selectBizOutboundList(bizOutbound);

        if(!CollectionUtils.isEmpty(list)){
            list.forEach(outbound->{
                BizOutboundProduct bizOutboundProduct = new BizOutboundProduct();
                bizOutboundProduct.setOutboundId(outbound.getId());
                List<BizOutboundProduct> bizOutboundProductList = bizOutboundProductMapper.selectBizOutboundProductList(bizOutboundProduct);
                outbound.setBizOutboundProductList(bizOutboundProductList);
            });
        }
        return list;
    }

    /**
     * 新增出库申请
     * 
     * @param bizOutbound 出库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizOutbound(BizOutbound bizOutbound)
    {
        //状态为申请中
        bizOutbound.setStatus("1");
        bizOutbound.setCreateTime(DateUtils.getNowDate());
        int result = bizOutboundMapper.insertBizOutbound(bizOutbound);

        //插入从表
        if(!CollectionUtils.isEmpty(bizOutbound.getBizOutboundProductList())) {
        bizOutbound.getBizOutboundProductList().forEach(product->{
            product.setOutboundId(bizOutbound.getId());
            bizOutboundProductMapper.insertBizOutboundProduct(product);
        });
    }
        return result;
    }

    /**
     * 修改出库申请
     *
     * @param bizOutbound 出库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizOutbound(BizOutbound bizOutbound)
    {
        bizOutbound.setUpdateTime(DateUtils.getNowDate());
        //先删之前的出库产品记录然后再新增修改后的
        bizOutboundProductMapper.deleteBizOutboundProductByOutboundId(bizOutbound.getId());

        if(!CollectionUtils.isEmpty(bizOutbound.getBizOutboundProductList())) {
            bizOutbound.getBizOutboundProductList().forEach(product->{
                product.setOutboundId(bizOutbound.getId());
                bizOutboundProductMapper.insertBizOutboundProduct(product);

            });
        }

        return bizOutboundMapper.updateBizOutbound(bizOutbound);
    }


    /**
     * 同意通过出库申请后，修改状态和库存
     *
     * @param bizOutbound 出库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizOutbound2(BizOutbound bizOutbound)//只需要传id和状态
    {
        bizOutbound.setUpdateTime(DateUtils.getNowDate());
        int result = bizOutboundMapper.updateBizOutbound(bizOutbound);

        //查询从表记录
        BizOutboundProduct bizOutboundProduct = new BizOutboundProduct();
        bizOutboundProduct.setOutboundId(bizOutbound.getId());
        List<BizOutboundProduct> outboundProductList = bizOutboundProductMapper.selectBizOutboundProductList(bizOutboundProduct);

        //修改库存
        if(!CollectionUtils.isEmpty(outboundProductList)){
            outboundProductList.forEach(product->{
                //如果此时状态值为2
                if(bizOutbound.getStatus().equals("2")) {
                    //修改库存表中的产品数量
                    BizStock bizStock = new BizStock();
                    bizStock.setWarehouseId(bizOutbound.getWarehouseId());
                    bizStock.setProductId(Long.valueOf(product.getProductId()));
                    List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                    if (!CollectionUtils.isEmpty(bizStockList)) {
                        bizStock = bizStockList.get(0);
                        bizStock.setQuantity(bizStock.getQuantity() - product.getNumber());
                        bizStock.setAvailableQuantity(bizStock.getAvailableQuantity() - product.getNumber());
                        bizStockMapper.updateBizStock(bizStock);
                    }

                    //再次修改产品种类表中的产品数量
                    BizProductModel productModel = new BizProductModel();
                    productModel.setProductId(product.getProductId());
                    //productModel.setModel(product.getModel());
                    List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel.setQuantity(productModel1.getQuantity() - product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);

                }
            });
        }
        return result;

    }


    /**
     * 批量删除出库记录
     * 
     * @param ids 需要删除的出库记录主键
     * @return 结果
     */
    @Override
    public int deleteBizOutboundByIds(Long[] ids)
    {
        return bizOutboundMapper.deleteBizOutboundByIds(ids);
    }

    /**
     * 删除出库记录信息
     * 
     * @param id 出库记录主键
     * @return 结果
     */
    @Override
    public int deleteBizOutboundById(Long id)
    {
        return bizOutboundMapper.deleteBizOutboundById(id);
    }

        /**
         * 新增出库记录
         *
         * @param bizOutbound 出库记录
         * @return 结果
         */
        @Transactional
        @Override
        public int insertBizOutbound2(BizOutbound bizOutbound)
        {
            bizOutbound.setCreateTime(DateUtils.getNowDate());
            int result = bizOutboundMapper.insertBizOutbound(bizOutbound);

            //插入从表
            if(!CollectionUtils.isEmpty(bizOutbound.getBizOutboundProductList())) {
                bizOutbound.getBizOutboundProductList().forEach(product->{
                    product.setOutboundId(bizOutbound.getId());
                    bizOutboundProductMapper.insertBizOutboundProduct(product);

                    //修改库存表中的产品数量
                    BizStock bizStock = new BizStock();
                    bizStock.setWarehouseId(product.getWarehouseId());
                    bizStock.setProductId(Long.valueOf(product.getProductId()));
                    List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                    if(!CollectionUtils.isEmpty(bizStockList)){
                        bizStock = bizStockList.get(0);
                        bizStock.setQuantity(bizStock.getQuantity() - product.getNumber());
                        bizStockMapper.updateBizStock(bizStock);
                    }

                    //修改产品种类表中的产品数量
                    BizProductModel productModel = new BizProductModel();
                    productModel.setProductId(product.getProductId());
                    //productModel.setModel(product.getModel());
                    List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel1.setQuantity(productModel1.getQuantity() - product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);


                    //修改产品表数量
                    BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct1.setQuantity(bizProduct1.getQuantity() - product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct1);
                });
            }
            return result;
        }

        /**
         * 修改出库记录（原始)
         *
         * @param bizOutbound 出库记录
         * @return 结果
         */
        @Transactional
        @Override
        public int updateBizOutbound3(BizOutbound bizOutbound)
        {
            bizOutbound.setUpdateTime(DateUtils.getNowDate());

            //先查出之前的记录把库存还原
            BizOutboundProduct bizOutboundProduct = new BizOutboundProduct();
            bizOutboundProduct.setOutboundId(bizOutbound.getId());
            List<BizOutboundProduct> outboundProductList = bizOutboundProductMapper.selectBizOutboundProductList(bizOutboundProduct);
            if(!CollectionUtils.isEmpty(outboundProductList)) {
                outboundProductList.forEach(product->{
                    //                product.setOutboundId(bizOutbound.getId());
                    //                bizOutboundProductMapper.insertBizOutboundProduct(product);

                    //还原之前对库存表中物品数量的修改
                    BizStock bizStock = new BizStock();
                    bizStock.setWarehouseId(product.getWarehouseId());
                    bizStock.setProductId(Long.valueOf(product.getProductId()));
                    List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                    if(!CollectionUtils.isEmpty(bizStockList)){
                        bizStock = bizStockList.get(0);
                        bizStock.setQuantity(bizStock.getQuantity() + product.getNumber());
//                        bizStock.setAvailableQuantity(bizStock.getAvailableQuantity() + product.getNumber());
                        bizStockMapper.updateBizStock(bizStock);
                    }

                    //还原之前对产品种类表中数量的修改
                    BizProductModel productModel = new BizProductModel();
                    productModel.setProductId(product.getProductId());
                    //productModel.setModel(product.getModel());
                    List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel1.setQuantity(productModel1.getQuantity() + product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);

                    //修改产品表数量
                    BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct1.setQuantity(bizProduct1.getQuantity() + product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct1);
                });
            }

            //先删之前的出库产品记录然后再新增修改后的
            bizOutboundProductMapper.deleteBizOutboundProductByOutboundId(bizOutbound.getId());

            if(!CollectionUtils.isEmpty(bizOutbound.getBizOutboundProductList())) {
                bizOutbound.getBizOutboundProductList().forEach(product->{
                    product.setOutboundId(bizOutbound.getId());
                    bizOutboundProductMapper.insertBizOutboundProduct(product);

                    //再次修改库存表中的产品数量
                    BizStock bizStock = new BizStock();
                    bizStock.setWarehouseId(product.getWarehouseId());
                    bizStock.setProductId(Long.valueOf(product.getProductId()));
                    List<BizStock> bizStockList = bizStockMapper.selectBizStockList(bizStock);
                    if(!CollectionUtils.isEmpty(bizStockList)){
                        bizStock = bizStockList.get(0);
                        bizStock.setQuantity(bizStock.getQuantity() - product.getNumber());
//                        bizStock.setAvailableQuantity(bizStock.getAvailableQuantity() - product.getNumber());
                        bizStockMapper.updateBizStock(bizStock);
                    }

                    //再次修改产品种类表中的产品数量
                    BizProductModel productModel = new BizProductModel();
                    productModel.setProductId(product.getProductId());
                    //productModel.setModel(product.getModel());
                    List<BizProductModel> productModelList = bizProductModelMapper.selectBizProductModelList(productModel);
                    BizProductModel productModel1 = productModelList.get(0);
                    productModel1.setQuantity(productModel1.getQuantity() - product.getNumber());
                    bizProductModelMapper.updateBizProductModel(productModel1);

                    //修改产品表数量
                    BizProduct bizProduct1 = bizProductMapper.selectBizProductById(Long.valueOf(product.getProductId()));
                    bizProduct1.setQuantity(bizProduct1.getQuantity() - product.getNumber() );
                    bizProductMapper.updateBizProduct(bizProduct1);

                });
            }

            return bizOutboundMapper.updateBizOutbound(bizOutbound);
        }
}
